

<template>

    <el-form ref="assetFormRef" :model="props.assetForm" :class="form-item" :rules="rules" :size="size" status-icon>
      <el-row>
        <el-col :span="7">
          
          <el-form-item class="form-item" prop="assetCode" label="资产编号：" label-width="80px">
            <el-input v-model="props.assetForm.assetCode" autocomplete="off"/>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item class="form-item" prop="assetName" label="资产名称：" label-width="100px">
            <el-input v-model="props.assetForm.assetName" autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="category" label="资产类别：" label-width="60px">
            <el-input v-model="props.assetForm.category" autocomplete="off" />
          </el-form-item>
        </el-col> 
        
      </el-row>
      <el-row>
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="status" label="资产状态：" label-width="80px">
            <el-tree-select
              v-model="props.assetForm.status"
              :data="departmentdata"
              :props="treeProps"
              :render-after-expand="false"
            />
          </el-form-item>
        </el-col>  
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="purchaseDate" label="采购时间：" label-width="80px">
            <el-input v-model="props.assetForm.purchaseDate" autocomplete="off"/>
          </el-form-item>
        </el-col>   
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="warrantyExpiry" label="维保到期时间：" label-width="80px">
            <el-input v-model="props.assetForm.warrantyExpiry" autocomplete="off" />
          </el-form-item>
        </el-col>  
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="price" label="资产价格：" label-width="80px">
            <!-- <el-input v-model.number="props.assetForm.price" autocomplete="off" /> -->
            <el-input-number v-model="props.assetForm.price" :min="0" :step="0.01" />
          </el-form-item>
        </el-col>  
      </el-row>
      <el-row>
        <el-col :span="7">
          <el-form-item class="form-item" prop="location" label="存放地域：" label-width="80px">
            <el-input v-model="props.assetForm.location" autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="bizIp" label="业务IP：" label-width="80px">
            <el-input v-model="props.assetForm.bizIp" autocomplete="off"/>
          </el-form-item>
        </el-col>  
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="ibmcIp" label="管理IP：" label-width="90px">
            <el-input v-model="props.assetForm.ibmcIp" autocomplete="off"/>
          </el-form-item>
        </el-col> 
        <el-col :span="7">  
          <el-form-item  class="form-item" prop="custodian" label="资产负责人：" label-width="90px">
            <el-input v-model="props.assetForm.custodian" autocomplete="off"/>
          </el-form-item>
        </el-col> 
      </el-row>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="clear()">清空</el-button>
      <el-button type="primary" @click="submitForm">
        提交
      </el-button>
    </div>
</template>
<script setup>
import { defineProps, defineEmits,reactive, ref, onBeforeMount, toRefs, onMounted} from 'vue'
import {getDepartmentList} from "../../api/department.js"
import {getRolesList} from "../../api/roles.js"
// import {getAdList} from "../../api/ad.js"
import {base64Decode} from "../../utils/base64.js"
// import type { FormInstance, FormRules } from "element-plus"
import { ElForm, ElFormItem, ElInput, ElButton } from 'element-plus';
// 定义一个事件 'addSubmit'，当表单提交时触发
const emit = defineEmits(['addSubmit']);
// const userFormRef = ref<FormInstance>()
// 接收父组件传递的数据
const props = defineProps({
  assetForm: Object,
  departmentdata: Array
});
// const { assetFrom } = toRefs(props)
const assetFormRef=ref()
// 自定义验证方法，校验确认密码是否与密码一致
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请确认密码'));
  } else if (value !== props.userForm.password) {
    console.log("value===========",value,"---------------",props.userForm.password)
    callback(new Error('两次密码输入不一致'));
  } else {
    callback(); // 验证通过
  }
};
const data=reactive({
  rules: {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur'},
      { min: 8, message: '输入密码最少8位', trigger: 'blur' },
      //{ validator: validatePass, trigger: 'blur' },
    ],
    checkpass: [
      { required: true, message: '请确认密码', trigger: 'blur'},
      { min: 8, message: '输入密码最少8位', trigger: 'blur' },
      { validator: validateConfirmPassword, trigger: 'blur' }

    ],
  
  },
  // 部门数据
  // departmentdata: [],
  // 角色数据
  rolesdata:[],
  // AD域数据
  addata: [],
  // 企微数据
  wechatdata: [],
})
const size=ref("small")

const {rules,rolesdata,addata,wechatdata}=toRefs(data)
const clear =()=>{
  props.userForm.password="",
  props.userForm.checkpass="",
  userFormRef.value.resetFields()
}

// 配置树的属性映射
const treeProps = ref({
  label: 'name',  // 显示节点的名称
  value: 'id',    // 使用 id 作为节点的值
  children: 'children'  // 使用 children 属性来表示子节点
});
// 获取部门数据
const getRoleNames = async()=>{
  try {
    const response = await getRolesList()
    const result = response.data.data
    if (result.roleslist == '[]') {
            return [];
    } else {
      // base64Decode 解析base64数据,返回json.parse后数据
      const decodeData = await base64Decode(result.roleslist)
      data.rolesdata=decodeData
      console.log("=======--13133131331-------------------====",rolesdata)
    }   
  }
  catch(error) {
    console.log(error)
  }
};

// 获取AD用户数据
const getAdNames = async()=>{
  try {
    const response = await getAdList()
    const result = response.data.data
      if (result.adlist == '[]') {
              return [];
      } else {
        // base64Decode 解析base64数据,返回json.parse后数据
        const decodeData = await base64Decode(result.adlist)
        data.addata=decodeData
        console.log("=======--13133131331-------------------====",addata)
      }
  }
  catch(error) {
    console.log(error)
  }
};
// 提交
const submitForm = async () => {
  // const form = ref('userFormRef');
  assetFormRef.value.validate((valid) => {
    if (valid) {
      console.log('提交成功', props.assetForm);
      emit('addSubmit',props.assetForm)
    } else {
      console.log('表单验证失败');
    }
  });
};
/*const getDepartmentData=()=>{
      getDepartmentList().then((response)=>{
        const result = response.data.data
        if (result.departmentlist == '[]') {
                return [];
        } else {
            const base64String = result.departmentlist
            const decodedBytes = atob(base64String);
            // 将字节串转换为 Uint8Array
            const uint8Array = new Uint8Array(decodedBytes.split('').map(c => c.charCodeAt(0)));
            // 使用 TextDecoder 解码为字符串
            const decoder = new TextDecoder("utf-8");
            const decodedString = decoder.decode(uint8Array);
            console.log("departmentinfo===========+++++++++",decodedString)
            //window.localStorage.setItem("menus",decodedString)
            //const md5Hash = md5(decodedString+'menus_list');
            //window.localStorage.setItem("md5Hash",md5Hash)
            departmentdata=JSON.parse(decodedString)
            console.log("departmentinfo=======---------------------====+++++aaaaassssss++++",departmentdata)
            // 存储菜单数据到 localStorage
            // const decodedString = window.localStorage.getItem('menus');
            // 返回解析后的菜单数据
            return JSON.parse(decodedString);
        }
      })
  }
  */
onMounted(()=>{
//  await getDepartmentData()
  // departmentdata = departmentlist
  getRoleNames()
  getAdNames()
  // console.log("departmentdata==========add==========",props.departmentdata)
})

</script>
<style scoped>
.form-item{
  width: 300px;
  margin: 10px auto;
}
</style>
